package com.bilicute.libman.service.impl;


import com.bilicute.libman.pojo.VerifyCode;
import com.bilicute.libman.service.IVerifyCodeGen;
import com.bilicute.libman.util.RandomUtils;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @所属包名: com.bilicute.libman.service.impl
 * @类名: IVerifyCodeGenImpl
 * @作者: i囡漫笔
 * @描述:
 * @创建时间: 2024-06-21 13:54
 */


@Service
public class IVerifyCodeGenImpl implements IVerifyCodeGen {

    private static final String[] FONT_TYPES = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" };

    private static final int VALICATE_CODE_LENGTH = 6;


    /**
     * @param graphics: 图形整体图样，制图底
     * @param width:    宽
     * @param height:  高
     * @author i囡漫笔
     * @description 设置背景颜色及大小，干扰线
     * @date 2024/6/21
     */
    private static void fillBackground(Graphics graphics, int width, int height) {
// 填充背景
        graphics.setColor(Color.WHITE);
//设置矩形坐标x y 为0
        graphics.fillRect(0, 0, width, height);

// 加入干扰线条
        for (int i = 0; i < 8; i++) {
//设置随机颜色算法参数
            graphics.setColor(RandomUtils.randomColor(40, 150));
            Random random = new Random();
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            graphics.drawLine(x, y, x1, y1);
        }
    }

    /**
     * @param width: 生成验证码的宽
     * @param height: 生成验证码的高
     * @param os:  二进制流
     * @return String
     * @author i囡漫笔
     * @description 生成验证码字符串，并将流存入栈
     * @date 2024/6/21
     */
    @Override
    public String generate(int width, int height, OutputStream os) throws IOException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        fillBackground(graphics, width, height);
        String randomStr = RandomUtils.randomString(VALICATE_CODE_LENGTH);
        createCharacter(graphics, randomStr);
        graphics.dispose();
//设置JPEG格式
        ImageIO.write(image, "JPEG", os);
        return randomStr;
    }

    /**
     * @param width: 生成图形验证码的宽
     * @param height:  生成验证码的高
     * @return VerifyCode
     * @author i囡漫笔
     * @description 生成图形验证码
     * @date 2024/6/21
     */
    @Override
    public VerifyCode generate(int width, int height) {
        VerifyCode verifyCode;
        try (
//将流的初始化放到这里就不需要手动关闭流
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream()
        ) {
            String code = generate(width, height, byteStream);
            verifyCode = new VerifyCode();
            verifyCode.setCode(code);
            verifyCode.setImgBytes(byteStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace(System.out);
            verifyCode = null;
        }
        return verifyCode;
    }



    /**
     * @param g: 颜色参数
     * @param randomStr:  随机出的字符串
     * @author i囡漫笔
     * @description 设置字体颜色大小
     * @date 2024/6/21
     */
    private void createCharacter(Graphics g, String randomStr) {
        char[] charArray = randomStr.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
//设置RGB颜色算法参数
            g.setColor(new Color(50 + RandomUtils.nextInt(100),
                    50 + RandomUtils.nextInt(100), 50 + RandomUtils.nextInt(100)));
//设置字体大小，类型
            g.setFont(new Font(FONT_TYPES[RandomUtils.nextInt(FONT_TYPES.length)], Font.BOLD, 26));
//设置x y 坐标
            g.drawString(String.valueOf(charArray[i]), 15 * i + 5, 19 + RandomUtils.nextInt(8));
        }
    }
}
